package edu.cmu.ibill.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.mybeans.dao.DAOException;
import org.mybeans.form.FormBeanException;
import org.mybeans.form.FormBeanFactory;

import edu.cmu.ibill.databeans.PersonalBill;
import edu.cmu.ibill.databeans.User;
import edu.cmu.ibill.formbeans.AddPersonalBillForm;
import edu.cmu.ibill.model.CategoryDictDAO;
import edu.cmu.ibill.model.Model;
import edu.cmu.ibill.model.PersonalBillDAO;

public class AddBillAction extends Action {

	private FormBeanFactory<AddPersonalBillForm> formBeanFactory = FormBeanFactory
			.getInstance(AddPersonalBillForm.class);
	PersonalBillDAO personalbillDAO;
	CategoryDictDAO catDAO;

	SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");

	public AddBillAction(Model model) {
		this.personalbillDAO = model.getPersonalBillDAO();
		this.catDAO = model.getCategoryDictDAO();
	}

	@Override
	public String getName() {
		return "addbill.do";
	}

	@Override
	public String perform(HttpServletRequest request) {
		// TODO Auto-generated method stub
		List<String> errors = new ArrayList<String>();
		request.setAttribute("addpersonalbillerrors", errors);
		request.setAttribute("categorydict", catDAO.getAllCategoryNames());
		User user = (User) request.getSession(false).getAttribute("user");
		try {
			AddPersonalBillForm form = formBeanFactory.create(request);

			request.setAttribute("date", form.getDate());
			request.setAttribute("amount", form.getAmount());
			request.setAttribute("category", form.getCategory());
			request.setAttribute("comment", form.getComment());

			errors.addAll(form.getValidationErrors());
			if (errors.size() != 0) {
				PersonalBill[] pBills = personalbillDAO.LookupByUserId(user
						.getId());
				request.setAttribute("personalbills", pBills);
				request.setAttribute("addpersonalbillinprocess", "");
				return "manage.jsp";
			}

			PersonalBill personalbill = new PersonalBill();
			if (catDAO.LookupCategoryName(Integer.parseInt(form.getCategory()))
					.equals("income"))
				personalbill.setAmount(-Math.abs(Double.parseDouble(form
						.getAmount())));
			else
				personalbill.setAmount(Double.parseDouble(form.getAmount()));
			personalbill.setCategoryId(Integer.parseInt(form.getCategory()));
			personalbill.setDate(new java.sql.Date(format.parse(form.getDate())
					.getTime()));
			personalbill.setDescription(form.getComment());
			personalbill.setIsShared(false);
			personalbill.setUserId(user.getId());

			personalbillDAO.create(personalbill);

			PersonalBill[] pBills = personalbillDAO
					.LookupByUserId(user.getId());
			request.setAttribute("personalbills", pBills);

			return "manage.jsp";

		} catch (DAOException e) {
			errors.add(e.getMessage());
			PersonalBill[] pBills = personalbillDAO
					.LookupByUserId(user.getId());
			request.setAttribute("personalbills", pBills);
			request.setAttribute("addpersonalbillinprocess", "");
			return "manage.jsp";
		} catch (FormBeanException e) {
			errors.add(e.getMessage());
			PersonalBill[] pBills = personalbillDAO
					.LookupByUserId(user.getId());
			request.setAttribute("personalbills", pBills);
			request.setAttribute("addpersonalbillinprocess", "");
			return "manage.jsp";
		} catch (ParseException e) {
			errors.add(e.getMessage());
			PersonalBill[] pBills = personalbillDAO
					.LookupByUserId(user.getId());
			request.setAttribute("personalbills", pBills);
			request.setAttribute("addpersonalbillinprocess", "");
			return "manage.jsp";
		}
	}

}
